package com.etrans.web.gps.business.report.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.report.domain.AlarmInfoVO;
import com.etrans.web.gps.common.MGTemplete;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.DateUtil;

/**
 *报警综合分析Service
 * @author zhangw
 *
 */
@Service
public class AlarmSynAnalyService extends BaseService{
	/**
	 * 根据报警类型按照日期查询车辆数
	 * @param id	报警类型id
	 * @param days  查询报警的日期列表
	 * @return
	 */
	public Integer queryAlarmVeCountByType(Integer id, List<String> days) {
		Integer alarmVeCount = 0;
		HashMap<String, Object> whereMap = new HashMap<String, Object>();
/*		whereMap.put("", "")
		for (String string : days) {
			
		}*/
		return null;
	}
	/**
	 * 根据车牌模糊查询报警车辆列表
	 * @return
	 */
	public List<AlarmInfoVO> queryAlarmVeList(String queryTime) {
		String tableName = "alarm_info_"+queryTime;
		//加入查询条件whereMap
		HashMap<String,Object> alarm_type = new HashMap<String, Object>();
		alarm_type.put("$gte", 0);
		HashMap<String,Object> whereMap = new HashMap<String, Object>();
		whereMap.put("alarm_type", alarm_type);
		//加入排序条件sortMap
		HashMap<String,Object> sortMap = new HashMap<String,Object>();
		sortMap.put("vehicle_no", 1);
		List<AlarmInfoVO> alarmVeList = MGTemplete.find("etalarm", tableName,whereMap,sortMap,AlarmInfoVO.class);
		return alarmVeList;
	}
	
	/**
	 * 当查询起止时间在同一天时
	 * @return  可直接进行分页查询
	 */
	public PageVo getAlarmVehiclePage(Map<String, Object> paramMap ,String tableName) {
		PageVo pageVo = new PageVo();
		// 处理查询条件
		HashMap<String, Object> whereMap = new HashMap<String, Object>();
		HashMap<String, Object> whereMap_Time = new HashMap<String, Object>();
		HashMap<String, Object> sortMap = null;
		int pageSize = 10;
		int page = 1;
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			pageSize = Integer.parseInt(String.valueOf(paramMap.get("pageSize")));
			page = Integer.parseInt(String.valueOf(paramMap.get("page")));
			String vehicleNo = (String) paramMap.get("queryCriteriaSql");
			String alarmType = (String) paramMap.get("alarmType");
			String start_Time = (String) paramMap.get("startTime");
			String end_Time = (String) paramMap.get("endTime");
			//确定查询条件
			if (null != vehicleNo && !"".equals(vehicleNo)) {
				//whereMap_No.put("$regex", vehicleNo);
				whereMap.put("vehicle_no", vehicleNo);
			}
			if (null != alarmType && !"".equals(alarmType)) {
				//whereMap_No.put("$regex", vehicleNo);
				whereMap.put("alarmType", alarmType);
			}
			if (null != start_Time && !"".equals(start_Time)) {
				whereMap_Time.put("$gte",  format.parse(start_Time)); // 大于
			}
			if (null != end_Time && !"".equals(end_Time)) {
				whereMap_Time.put("$lte", format.parse(end_Time)); // 小于
			}
			whereMap.put("alarm_time", whereMap_Time);
			// 加入排序条件sortMap
			sortMap = new HashMap<String, Object>();
			sortMap.put("alarm_time", -1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Long total = MGTemplete.findCount("etalarm", tableName, whereMap);
		List<AlarmInfoVO> list = MGTemplete.find("etalarm", tableName, whereMap, sortMap,page,pageSize,AlarmInfoVO.class);
		pageVo.setTotal(total.intValue());
		pageVo.setRows(list);
		pageVo.setPage(page);
		return pageVo;
	}

	/**
	 * 查询指定车辆的历史报警记录列表
	 */
	public List<AlarmInfoVO>  getAlarmVehicleList(Map<String, Object> paramMap ,String tableName) {
		// 处理查询条件
		HashMap<String, Object> whereMap = new HashMap<String, Object>();
		HashMap<String, Object> whereMap_Time = new HashMap<String, Object>();
		HashMap<String, Object> sortMap = null;
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String vehicleNo = (String) paramMap.get("queryCriteriaSql");
			String alarmType = (String) paramMap.get("alarmType");
			String start_Time = (String) paramMap.get("startTime");
			String end_Time = (String) paramMap.get("endTime");
			//确定查询条件
			if (null != vehicleNo && !"".equals(vehicleNo)) {
				//whereMap_No.put("$regex", vehicleNo);
				whereMap.put("vehicle_no", vehicleNo);
			}
			if (null != alarmType && !"".equals(alarmType)) {
				//whereMap_No.put("$regex", vehicleNo);
				whereMap.put("alarmType", alarmType);
			}
			if (null != start_Time && !"".equals(start_Time)) {
				whereMap_Time.put("$gte",  format.parse(start_Time)); // 大于
			}
			if (null != end_Time && !"".equals(end_Time)) {
				whereMap_Time.put("$lte", format.parse(end_Time)); // 小于
			}
			whereMap.put("alarm_time", whereMap_Time);
			// 加入排序条件sortMap
			sortMap = new HashMap<String, Object>();
			sortMap.put("alarm_time", -1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<AlarmInfoVO> list = MGTemplete.find("etalarm", tableName, whereMap, sortMap,AlarmInfoVO.class);
		return list;
	}
	
	/**
	 * 查询指定车辆的历史报警记录数量
	 */
	public Integer getAlarmVehicleListCount(Map<String, Object> paramMap ,String tableName) {
		HashMap<String, Object> whereMap = new HashMap<String, Object>();
		HashMap<String, Object> whereMap_Time = new HashMap<String, Object>();
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String vehicleNo = (String) paramMap.get("queryCriteriaSql");
			String alarmType = (String) paramMap.get("alarmType");
			String start_Time = (String) paramMap.get("startTime");
			String end_Time = (String) paramMap.get("endTime");
			//确定查询条件
			if (null != vehicleNo && !"".equals(vehicleNo)) {
				//whereMap_No.put("$regex", vehicleNo);
				whereMap.put("vehicle_no", vehicleNo);
			}
			if (null != alarmType && !"".equals(alarmType)) {
				//whereMap_No.put("$regex", vehicleNo);
				whereMap.put("alarmType", alarmType);
			}
			if (null != start_Time && !"".equals(start_Time)) {
				whereMap_Time.put("$gte",  format.parse(start_Time)); // 大于
			}
			if (null != end_Time && !"".equals(end_Time)) {
				whereMap_Time.put("$lte", format.parse(end_Time)); // 小于
			}
			whereMap.put("alarm_time", whereMap_Time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Long total = MGTemplete.findCount("etalarm", tableName, whereMap);
		return total.intValue();
	}
	
}
